package com.heima.article.listener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
 * @作者 itcast
 * @创建日期 2021/4/19 15:31
 **/
@Component
@Slf4j
public class ArticleIsDownListener {
    @Autowired
    ApArticleConfigService apArticleConfigService;
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMessage(String message){
        log.info("接受到文章上下架 主题的消息,消息内容: {}",message);
        Map map = JSON.parseObject(message, Map.class);
        // 获取文章id
        Long articleId = (Long)map.get("articleId");
        Integer enable = (Integer)map.get("enable"); //是否上架  0 下架  1 上架
        // 修改文章配置信息
        boolean isDown = enable.intValue() == 0 ? true : false;
        apArticleConfigService.update(
                Wrappers.<ApArticleConfig>lambdaUpdate()
                        .set(ApArticleConfig::getIsDown,isDown)
                        .eq(ApArticleConfig::getArticleId,articleId)
        );
    }
}
